package javacourse.mq.kafka.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * Create by chenhd on 2021/10/31.
 */
@Service
public class Producer {

    @Autowired
    KafkaTemplate kafkaTemplate;

    public void send(String topic, String msg) {
        System.out.println(String.format("------------------produce message [%s] to topic [%s]", msg, topic));
        ListenableFuture listenableFuture = kafkaTemplate.send(topic, msg);

        listenableFuture.addCallback(new ListenableFutureCallback() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("message send fail.");
            }

            @Override
            public void onSuccess(Object o) {
                System.out.println("message send success.");
            }
        });
    }
}
